package com.boot.web.action;

import java.util.LinkedList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boot.beans.UserMessage;
import com.boot.common.BaseAction;
import com.boot.web.service.UserMessageService;

import util.Constant;
import util.DateUtil;
import util.FieldUtil;
import util.MessageUtil;
import util.Page;

@Controller
@RequestMapping("/sys")
public class UserMessageAction extends BaseAction {
	private String							actionname	= "用户反馈";
	private String							actionclass	= "UserMessage";
	@Autowired
	private UserMessageService	service;

	@RequestMapping(value = "/add2UserMessage.do")
	public String add2() {
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addUserMessage";
	}

	@RequestMapping(value = "/getUserMessage.do")
	public String get(int uid) {
		try {
			UserMessage bean = service.get(uid);
			request.setAttribute("modifybean", bean);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyUserMessage";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}
	@RequestMapping(value = "/getUserMessageAll.do")
	public String getUserMessageAll(int uid) {
		try {
			UserMessage bean = service.get(uid);
			request.setAttribute("modifybean", bean);
			
			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "getUserMessageAll";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addUserMessage.do")
	public String add(UserMessage bean) {
		try {
			bean.setUserId(getSimpleUser().getId());
			bean.setAddDate(DateUtil.getCurrentTime());
			service.add(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateUserMessage.do")
	public String update(UserMessage bean) {
		try {
			bean.setUserId(getSimpleUser().getId());
			bean.setAddDate(DateUtil.getCurrentTime());
			service.update(bean);
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryUserMessage.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, UserMessage.class, parmnames, parmvalues);

			parmnames.add("userId");
			parmvalues.add(getSimpleUser().getId());

			Page page = service.selectPage(p, UserMessage.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listUserMessage";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	@RequestMapping(value = "/queryUserMessageAll.do")
	public String queryUserMessageAll() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, UserMessage.class, parmnames, parmvalues);
			Page page = service.selectPage(p, UserMessage.class);
			session.setAttribute(Constant.SESSION_PAGE, page);
			
			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "queryUserMessageAll";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteUserMessage.do")
	public String delete(String ids) {
		try {
			service.delete(ids);
			MessageUtil.addRelMessage(request, "删除信息成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "删除信息失败.");
			return ERROR;
		}
	}

}
